/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ps.yj.y1j0.data;

import org.apache.commons.beanutils.BeanMap;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Set;

/**
 * @author yanjun
 */
public abstract class YJDataObject implements YJIData {
    @Override
    public Object get(String name) {
        String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
        try {
            Method method = this.getClass().getMethod(methodName);
            return method.invoke(this);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String[] properties() {
        BeanMap beanMap = new BeanMap(this);
        Set keySet = beanMap.keySet();
        int size = keySet.size(), i = 0;
        String[] names = new String[size];
        for (Object key : keySet) {
            names[i++] = key.toString();
        }
        return names;
    }
}
